﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;


namespace ADail
{
    class CallOptions
    {
        // Reference to the Outlook Explorer object to which we are attached.
        private Outlook.Explorer _explorer;

        // True if this object is attached to an Explorer window.
        private bool _attached;

        // Reference to the parent collection.
        private List<CallOptions> _parent;

        // Explorer command bar members.
        private const string DEFAULT_COMMANDBAR_NAME = "ShortcutBar";
        private Office.CommandBar       _shortcutBar;
        private Office.CommandBarButton _mailButton;
        private Office.CommandBarButton _contButton;
        private Office.CommandBarButton _apptButton;
        private Office.CommandBarButton _taskButton;

        /// <summary>
        /// ExplorerUI constructor.
        /// </summary>
        /// <param name="explorer">Explorer window to attach to</param>
        /// <param name="parent">Parent list object</param>
        /// <param name="attachOnActivate">True to attach on activate event, false to attach immediately</param>
        internal CallOptions(Outlook.Explorer explorer, List<CallOptions> parent, bool attachOnActivate)
        {
            _explorer = explorer;
            _parent   = parent;
            _attached = false;

            // Subscribe to the Explorer close event.
            Outlook.ExplorerEvents_10_Event explorerEvents = (Outlook.ExplorerEvents_10_Event)_explorer;
            explorerEvents.Close += delegate()
            {
                // Detach from the Explorer window and remove
                // this object from the parent collection.
                Detach();
                _parent.Remove( this );
            };

            if ( !attachOnActivate )
            {
                // Attach immediately.
                Attach();
            }
            else
            {
                // Defer the attach call until the activate event.
                explorerEvents.Activate += delegate()
                {
                    if ( !_attached ) Attach();
                };
            }


            

        }

        /// <summary>
        /// Reference to the attached Explorer window.
        /// </summary>
        internal Outlook.Explorer Explorer
        {
            get { return _explorer; }
        }

        /// <summary>
        /// True if attached to an Explorer window.
        /// </summary>
        internal bool Attached
        {
            get { return _attached; }
        }

        /// <summary>
        /// Creates the ShortcutBar and adds it to the attached Explorer UI.
        /// </summary>
        private void Attach()
        {
            // If we are already attached, just return.
            if ( _attached ) return;

            object missing = System.Reflection.Missing.Value;

            // If the Explorer is not yet initialized, the following
            // call fails. In this case, add the user interface
            // when the Explorer is activated.
            Office.CommandBars cmdBars = _explorer.CommandBars;

            // Verify that the ShortcutBar is not already loaded.
            foreach ( Office.CommandBar cmdBar in cmdBars )
            {
                if ( cmdBar.Name.Equals( DEFAULT_COMMANDBAR_NAME ) )
                {
                    cmdBar.Delete();
                    break;
                }
            }

            // Create the ShortcutBar command bar.
            _shortcutBar = cmdBars.Add( DEFAULT_COMMANDBAR_NAME, Office.MsoBarPosition.msoBarTop, false, true );

            // Add a New Mail button.
            _mailButton = (Office.CommandBarButton)_shortcutBar.Controls.Add( Office.MsoControlType.msoControlButton, missing, missing, 1, 1 );
            _mailButton.Style = Office.MsoButtonStyle.msoButtonIconAndCaption;
            _mailButton.Caption = "New Mail";
            _mailButton.FaceId = 24;
            _mailButton.TooltipText = "Create a new mail message";
            _mailButton.Click += new Office._CommandBarButtonEvents_ClickEventHandler( OnNewMailButtonClick );

            // Add a New Contact button.
            _contButton = (Office.CommandBarButton)_shortcutBar.Controls.Add( Office.MsoControlType.msoControlButton, missing, missing, 2, 1 );
            _contButton.Style = Office.MsoButtonStyle.msoButtonIconAndCaption;
            _contButton.Caption = "New Contact";
            _contButton.FaceId = 1099;
            _contButton.TooltipText = "Create a new contact";
            _contButton.Click += new Office._CommandBarButtonEvents_ClickEventHandler( OnNewContactButtonClick );

            // Add a New Appointment button.
            _apptButton = (Office.CommandBarButton)_shortcutBar.Controls.Add( Office.MsoControlType.msoControlButton, missing, missing, 3, 1 );
            _apptButton.Style = Office.MsoButtonStyle.msoButtonIconAndCaption;
            _apptButton.Caption = "New Appointment";
            _apptButton.FaceId = 1098;
            _apptButton.TooltipText = "Create a new appointment";
            _apptButton.Click += new Office._CommandBarButtonEvents_ClickEventHandler( OnNewAppointmentButtonClick );

            // Add a New Task button.
            _taskButton = (Office.CommandBarButton)_shortcutBar.Controls.Add( Office.MsoControlType.msoControlButton, missing, missing, 4, 1 );
            _taskButton.Style = Office.MsoButtonStyle.msoButtonIconAndCaption;
            _taskButton.Caption = "New Task";
            _taskButton.FaceId = 1100;
            _taskButton.TooltipText = "Create a new task";
            _taskButton.Click += new Office._CommandBarButtonEvents_ClickEventHandler( OnNewTaskButtonClick );

            // Turn on the command bar.
            _shortcutBar.Visible = true;
            _shortcutBar.Enabled = true;

            _attached = true;
        }

        /// <summary>
        /// Removes the ShortcutBar command bar from the associated Explorer.
        /// </summary>
        internal void Detach()
        {
            // If we aren't attached, just return.
            if ( !_attached ) return;

            // Remove the ShortcutBar from the explorer.
            if ( _shortcutBar != null )
            {
                try
                {
                    // Delete may throw an exception if the underlying
                    // command bar has been manually deleted by the user.
                    _shortcutBar.Delete();
                }
                finally
                {
                    _shortcutBar = null;
                }
            }

            _attached = false;
        }

        private void OnNewMailButtonClick( Office.CommandBarButton Ctrl, ref bool CancelDefault )
        {
            // Create a new Mail item and display it to the user.
            Outlook.Application outlookApp = (Outlook.Application)Ctrl.Application;
            Outlook.MailItem newMail = (Outlook.MailItem)outlookApp.CreateItem( Outlook.OlItemType.olMailItem );
            newMail.Display( false );
        }

        private void OnNewTaskButtonClick( Office.CommandBarButton Ctrl, ref bool CancelDefault )
        {
            // Create a new Task item and display it to the user.
            Outlook.Application outlookApp = (Outlook.Application)Ctrl.Application;
            Outlook.TaskItem newTask = (Outlook.TaskItem)outlookApp.CreateItem( Outlook.OlItemType.olTaskItem );
            newTask.Display( false );
        }

        private void OnNewAppointmentButtonClick( Office.CommandBarButton Ctrl, ref bool CancelDefault )
        {
            // Create a new Appointment item and display it to the user.
            Outlook.Application outlookApp = (Outlook.Application)Ctrl.Application;
            Outlook.AppointmentItem newAppt = (Outlook.AppointmentItem)outlookApp.CreateItem( Outlook.OlItemType.olAppointmentItem );
            newAppt.Display( false );
        }

        private static void OnNewContactButtonClick( Office.CommandBarButton Ctrl, ref bool CancelDefault )
        {
            // Create a new Contact item and display it to the user.
            Outlook.Application outlookApp = (Outlook.Application)Ctrl.Application;
            Outlook.ContactItem newCont = (Outlook.ContactItem)outlookApp.CreateItem( Outlook.OlItemType.olContactItem );
            newCont.Display( false );
        }
    }


}
